package com.login.serviceimpl;

import com.login.dao.UserDao;
import com.login.pojo.User;
import com.login.service.UserService;
import com.login.service.ex.UsernameDuplicateException;
import com.login.service.ex.UsernameNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("serviceForLogin")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    //用户注册业务逻辑
    @Override
    public void reg(User user) {
        System.out.print(user.getUsername());
        System.out.print(user.getPassword());
        System.out.print(user.getType());
        String username = user.getUsername();
        String password=user.getPassword();
        Integer type=user.getType();
        User result = userDao.find(username,password,type);
        if(result != null && username.equals(result.getUsername())){
            throw new UsernameDuplicateException("用户名被占用");
        }
        user.setUsername(username);
        user.setPassword(password);
        user.setType(type);
        System.out.println(result);
        userDao.insert(user);
    }
    //用户登录业务逻辑
    @Override
    public User login(String username, String password,Integer type) {
        User result=userDao.find(username,password,type);
        if(result == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        User user =new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setType(type);
        return user;
    }
}

